package com.itita.ww2.handler.requesthandler.worldmap;

import java.util.TimeZone;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import com.itita.ww2.core.AbstractClientRequestHandler;
import com.itita.ww2.core.TaskPerSecond;
import com.itita.ww2.core.TaskPerSecond.ITimerCondition;
import com.itita.ww2.data.WorldMapConfig;
import com.smartfoxserver.v2.annotations.MultiHandler;
import com.smartfoxserver.v2.entities.data.ISFSObject;

@MultiHandler
public class WorldMapHandler extends AbstractClientRequestHandler {
	public static final String COMMAND_PREFIX = "worldmap"; // 前缀

	/**
	 * 初始化世界地图
	 */
	private void initialize() {
		// 添加一个任务/每天 endTime 结算
		ISFSObject time = WorldMapConfig.getCityTimeCfg();
		final String[] endTime = time.getUtfString("end").split(":");
		final String timeZoneStr = time.getUtfString("timezone");
		final TimeZone tz = TimeZone.getTimeZone(timeZoneStr);
		TaskPerSecond.addTask("wm_dailyReckoning", new ITimerCondition() {

			@Override
			public boolean condition() {
				int endHours = Math.min(Integer.valueOf(endTime[0]), 23);
				int endMinute = Math.min(Integer.valueOf(endTime[1]), 59);
				DateTime nowTimeWithZone = DateTime.now(DateTimeZone.forTimeZone(tz));
				DateTime endTimeWithZone = nowTimeWithZone.withHourOfDay(endHours).withMinuteOfHour(endMinute);
				return endTimeWithZone.isEqual(nowTimeWithZone);
			}

		}, new DailyReckoningTimeEnd(), false);
	}

	@Override
	protected void init() {
		// 初始化
		initialize();
		/*
		 * Client requests
		 */
		addClientRequestItem("getworldmapdata", GetWorldMapData.class);// 获取大地图数据(城池列表/区块列表)
		addClientRequestItem("getcitybaselist", GetCityBaseList.class);// 获取某城池内据点列表
		addClientRequestItem("getbasemap", GetBaseMap.class);// 获取指定据点地图
		addClientRequestItem("getbasedata", GetBaseData.class);// 获取指定据点详细数据
		addClientRequestItem("basebattlestart", BaseBattleStart.class);// 攻击据点开始
		addClientRequestItem("basebattleend", BaseBattleEnd.class);// 攻击据点结束
		addClientRequestItem("declarwar", DeclarWar.class);// 对城池进行宣战
		addClientRequestItem("gettrend", GetTrend.class);// 获取大势
		addClientRequestItem("explore", ExploreBlock.class);// 探索某区块
		addClientRequestItem("listallianceranking", ListAllianceRanking.class);// 获取联盟排行榜
		addClientRequestItem("listuserranking", ListUserRanking.class);// 获取用户排行榜
		addClientRequestItem("base_lv_donate", BaseLvDonate.class); // 据点升级捐献
		addClientRequestItem("base_en_donate", BaseEnDonate.class); // 据点强化捐献		
	}

	@Override
	protected void setCommandPrefix() {
		super.cmd_prefix = COMMAND_PREFIX;
	}

}
